/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLEncoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.Connection;
/*     */ import org.jsoup.Jsoup;
/*     */ import org.jsoup.nodes.Document;
/*     */ import org.jsoup.nodes.Element;
/*     */ import org.jsoup.select.Elements;
/*     */ 
/*     */ public class ListSearchEngineResult extends AbstractAction
/*     */ {
/*     */   public static final String BAIDU_URL = "http://www.baidu.com/s?wd=";
/*     */   public static final String USERAGENT = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Tablet PC 2.0)";
/*  41 */   private static Logger logger = Logger.getLogger(ListSearchEngineResult.class.getName());
/*     */ 
/*     */   public ListSearchEngineResult(AbstractHttpServlet servlet)
/*     */   {
/*  51 */     super(servlet);
/*     */   }
/*     */ 
/*     */   private List<Map<String, String>> getSearchResult(String keyword) {
/*  55 */     List result = new ArrayList();
/*     */     try {
/*  57 */       String url = "http://www.baidu.com/s?wd=" + URLEncoder.encode(keyword, "utf-8");
/*     */       try {
/*  59 */         Document doc = Jsoup.connect(url).userAgent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Tablet PC 2.0)")
/*  60 */           .timeout(10000).get();
/*     */ 
/*  62 */         Elements elements = doc
/*  63 */           .select("div[class^=result c-container]");
/*  64 */         if (elements != null)
/*  65 */           for (Element element : elements) {
/*  66 */             String title = getHTML(element, "h3 a");
/*  67 */             String urlLink = null;
/*  68 */             if ((element.select("h3 a") != null) && 
/*  69 */               (element.select("h3 a").first() != null)) {
/*  70 */               urlLink = element.select("h3 a").first()
/*  71 */                 .absUrl("href");
/*     */             }
/*     */ 
/*  74 */             String desc = getHTML(element, "div[class=c-abstract]");
/*  75 */             if ((desc == null) || (desc.length() == 0)) {
/*  76 */               desc = getHTML(element, "div");
/*     */             }
/*  78 */             if ((desc != null) && (desc.length() > 0)) {
/*  79 */               int idx = desc.indexOf("...");
/*  80 */               if (idx > 0) {
/*  81 */                 desc = desc.substring(0, idx);
/*     */               }
/*     */             }
/*     */ 
/*  85 */             Map item = new HashMap();
/*  86 */             item.put("title", title);
/*  87 */             item.put("urlLink", urlLink);
/*  88 */             item.put("desc", desc);
/*  89 */             result.add(item);
/*     */           }
/*     */       }
/*     */       catch (IOException e)
/*     */       {
/*  94 */         logger.warn("get company info from baidu failed", e);
/*     */       }
/*     */     } catch (UnsupportedEncodingException e) {
/*  97 */       logger.warn("get company info from baidu failed", e);
/*     */     }
/*  99 */     return result;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/* 106 */     String keyword = Util.getURLParameter(request.getParameter("keyword"));
/*     */ 
/* 108 */     List result = getSearchResult(keyword);
/* 109 */     response.setContentType("text/html;charset=utf-8");
/* 110 */     PrintWriter out = response.getWriter();
/* 111 */     JSONObject jsonObject = new JSONObject();
/* 112 */     jsonObject.put("error", "0");
/* 113 */     jsonObject.put("result", result);
/* 114 */     out.println(jsonObject);
/* 115 */     out.flush();
/* 116 */     out.close();
/*     */   }
/*     */ 
/*     */   private String getHTML(Element element, String path) {
/* 120 */     if (element == null) {
/* 121 */       return "";
/*     */     }
/* 123 */     Elements elements = element.select(path);
/* 124 */     if (elements == null) {
/* 125 */       return "";
/*     */     }
/* 127 */     Element first = elements.first();
/* 128 */     if (first != null) {
/* 129 */       return first.text();
/*     */     }
/* 131 */     return elements.text();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.ListSearchEngineResult
 * JD-Core Version:    0.6.1
 */